package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.ContractMasterBean;
import com.zhuoshi.utils.DbHelper;

public class ContractMasterDao {
	// 添加
	public int add(ContractMasterBean bean) {
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append(
					"insert into contractmaster (letternum,numerical,saleno,purch,customerid,condate,managerid,area,province,remarks) values");
			sql.append(" (?,?,?,?,?,?,?,?,?,?)");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getLetternum());
			pstat.setInt(2, bean.getNumerical());
			pstat.setString(3, bean.getSaleno());
			pstat.setString(4, bean.getPurch());
			pstat.setInt(5, bean.getCustomerid());
			pstat.setDate(6, bean.getCondate());
			pstat.setInt(7, bean.getManagerid());
			pstat.setInt(8, bean.getArea());
			pstat.setInt(9, bean.getProvince());
			pstat.setString(10, bean.getRemarks());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, null);
		}

		return rows;
	}

	// 修改
	public int update(ContractMasterBean bean) {
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("update contractmaster set ");
			sql.append("letternum=?,");
			sql.append("numerical=?,");
			sql.append("saleno=?,");
			sql.append("purch=?,");
			sql.append("customerid=?,");
			sql.append("condate=?,");
			sql.append("managerid=?,");
			sql.append("area=?,");
			sql.append("province=?,");
			sql.append("remarks=?");
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setString(1, bean.getLetternum());
			pstat.setInt(2, bean.getNumerical());
			pstat.setString(3, bean.getSaleno());
			pstat.setString(4, bean.getPurch());
			pstat.setInt(5, bean.getCustomerid());
			pstat.setDate(6, bean.getCondate());
			pstat.setInt(7, bean.getManagerid());
			pstat.setInt(8, bean.getArea());
			pstat.setInt(9, bean.getProvince());
			pstat.setString(10, bean.getRemarks());
			pstat.setInt(11, bean.getId());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, null);
		}

		return rows;
	}

	// 删除
	public int delete(int id) {
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("delete from contractmaster where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, null);
		}

		return rows;
	}

	/**
	 * 通过id查询 获取一个数据实体
	 * 
	 * @param id
	 *            自增ID
	 * @return
	 */
	public ContractMasterBean getModel(int id) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		ContractMasterBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from contractmaster where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while (rs.next()) {
				bean = new ContractMasterBean();
				bean.setId(rs.getInt("id"));
				bean.setLetternum(rs.getString("letternum"));
				bean.setNumerical(rs.getInt("numerical"));
				bean.setSaleno(rs.getString("saleno"));
				bean.setPurch(rs.getString("purch"));
				bean.setCustomerid(rs.getInt("customerid"));
				bean.setCondate(rs.getDate("condate"));
				bean.setManagerid(rs.getInt("managerid"));
				bean.setArea(rs.getInt("area"));
				bean.setProvince(rs.getInt("province"));
				bean.setRemarks(rs.getString("remarks"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return bean;
	}

	
	/**
	 * 通过numerical查询 获取一个数据实体
	 * 
	 * @param numerical
	 *            合同数字编号
	 * @return
	 */
	public ContractMasterBean getContractMasterBean(int numerical) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		ContractMasterBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from contractmaster where numerical=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, numerical);
			rs = pstat.executeQuery();
			while (rs.next()) {
				bean = new ContractMasterBean();
				bean.setId(rs.getInt("id"));
				bean.setLetternum(rs.getString("letternum"));
				bean.setNumerical(rs.getInt("numerical"));
				bean.setSaleno(rs.getString("saleno"));
				bean.setPurch(rs.getString("purch"));
				bean.setCustomerid(rs.getInt("customerid"));
				bean.setCondate(rs.getDate("condate"));
				bean.setManagerid(rs.getInt("managerid"));
				bean.setArea(rs.getInt("area"));
				bean.setProvince(rs.getInt("province"));
				bean.setRemarks(rs.getString("remarks"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return bean;
	}
	
	
	
	/**
	 * 查询多条 List
	 * 
	 * @param bean
	 *            条件 如果没有条件 传入null 查询所有
	 * @return
	 */
	public List<ContractMasterBean> getList(ContractMasterBean bean, String startDate, String endDate) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<ContractMasterBean> list = new ArrayList<ContractMasterBean>();
		try {
			conn = DbHelper.getConnection();
			// ---===================构建查询条件=================---//
			StringBuffer sql = new StringBuffer();
			sql.append("select * from contractmaster where 1=1");
			// if(bean!=null){

			if (null != bean.getNumerical()) {// 合同数字编号
				sql.append(" and numerical like '%" + bean.getNumerical() + "%'");
			}
			if (!"".equals(startDate) && startDate != null) {
				sql.append(" and condate>='" + startDate + "'");
			}

			if (!"".equals(endDate) && endDate != null) {
				sql.append(" and condate<='" + endDate + "'");
			}

			// }
			// ---===================/构建查询条件=================---//
			pstat = conn.prepareStatement(sql.toString());
			// System.out.println(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				ContractMasterBean cBean = new ContractMasterBean();
				cBean.setId(rs.getInt("id"));
				cBean.setLetternum(rs.getString("letternum"));
				cBean.setNumerical(rs.getInt("numerical"));
				cBean.setSaleno(rs.getString("saleno"));
				cBean.setPurch(rs.getString("purch"));
				cBean.setCustomerid(rs.getInt("customerid"));
				cBean.setCondate(rs.getDate("condate"));
				cBean.setManagerid(rs.getInt("managerid"));
				cBean.setArea(rs.getInt("area"));
				cBean.setProvince(rs.getInt("province"));
				cBean.setRemarks(rs.getString("remarks"));
				list.add(cBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}

	/**
	 * 验证合同编号是否重复  如果有则返回true  
	 * @param letternum
	 * @param numerical
	 * @return 数据库中是否有这个合同编号
	 */
	public boolean verifyNumerical(Integer numerical){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		boolean result = false;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select id from contractmaster where numerical=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, numerical);
			rs = pstat.executeQuery();
			while(rs.next()){
				result = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return result;
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	public List<ContractMasterBean> getAllList() {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<ContractMasterBean> list = new ArrayList<ContractMasterBean>();
		try {
			conn = DbHelper.getConnection();
			// ---===================构建查询条件=================---//
			StringBuffer sql = new StringBuffer();
			sql.append("select * from contractmaster where 1=1");
			
			// ---===================/构建查询条件=================---//
			pstat = conn.prepareStatement(sql.toString());
			// System.out.println(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				ContractMasterBean cBean = new ContractMasterBean();
				cBean.setId(rs.getInt("id"));
				cBean.setLetternum(rs.getString("letternum"));
				cBean.setNumerical(rs.getInt("numerical"));
				cBean.setSaleno(rs.getString("saleno"));
				cBean.setPurch(rs.getString("purch"));
				cBean.setCustomerid(rs.getInt("customerid"));
				cBean.setCondate(rs.getDate("condate"));
				cBean.setManagerid(rs.getInt("managerid"));
				cBean.setArea(rs.getInt("area"));
				cBean.setProvince(rs.getInt("province"));
				cBean.setRemarks(rs.getString("remarks"));
				list.add(cBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}

}
